package com.yuji.aacar.widgets.dialogue;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.yuji.aacar.R;


/**
 * Created by yehaijian on 16/6/28.
 */
public class MyDialog {
    private TextView text_tv;
    private TextView sureButton_tv;
    private TextView cancel_tv;
    private View view;
    private Context context;
    private boolean isReverse = false;

    /**
     * @param context
     */
    public MyDialog(Context context) {
        init(context);
    }

    public MyDialog(Context context, boolean isReverse) {
        init(context);
        this.isReverse = isReverse;
    }

    private void init(Context context) {
        this.context = context;
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.adviser_dialog, null, false);
        text_tv = (TextView) view.findViewById(R.id.adviser_dialog_text);
        sureButton_tv = (TextView) view.findViewById(R.id.adviser_dialog_bt);
        cancel_tv = (TextView) view.findViewById(R.id.adviser_dialog_cancel);
    }

    public void createDialog(String text, String sureText, final MyOnclickListener onClickListener) {
        text_tv.setText(text);
        sureButton_tv.setText(sureText);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(view);
        final AlertDialog alertDialog = builder.create();
        //确定
        sureButton_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isReverse) {
                    onClickListener.onClick(alertDialog);
                }
                alertDialog.dismiss();
            }
        });
        alertDialog.show();
        //取消
        cancel_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isReverse) {
                    onClickListener.onClick(alertDialog);
                }
                alertDialog.dismiss();
            }
        });
        alertDialog.getWindow().getDecorView().getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int height = alertDialog.getWindow().getDecorView().getMeasuredHeight();
                int width = alertDialog.getWindow().getDecorView().getMeasuredWidth();
                alertDialog.getWindow().setLayout((int) (width * 0.8), height);
                alertDialog.getWindow().getDecorView().getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
        alertDialog.getWindow().setBackgroundDrawableResource(R.drawable.adviser_dialog);
    }

    public TextView getText_tv() {
        return text_tv;
    }

    public void setText_tv(TextView text_tv) {
        this.text_tv = text_tv;
    }

    public TextView getSureButton_tv() {
        return sureButton_tv;
    }

    public void setSureButton_tv(TextView sureButton_tv) {
        this.sureButton_tv = sureButton_tv;
    }

    public TextView getCancel_tv() {
        return cancel_tv;
    }

    public void setCancel_tv(TextView cancel_tv) {
        this.cancel_tv = cancel_tv;
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public boolean isReverse() {
        return isReverse;
    }

    public void setReverse(boolean reverse) {
        isReverse = reverse;
    }

    public interface MyOnclickListener {
        void onClick(Dialog dialog);
    }
}
